using System;
using System.Threading;
using System.ComponentModel;
using System.Security.Principal;
using Microsoft.Practices.ServiceLocation;

namespace Caramel
{
	public static class CurrentUser
	{
		[EditorBrowsable(EditorBrowsableState.Never)]
		public static readonly string CurrentUserSourceKey = "CurrentUserSource";

		public static Func<IIdentity> Identity = 
			() =>
			{
				var source = ServiceLocator.Current.TryGetInstance<ISource<IIdentity>>(CurrentUser.CurrentUserSourceKey);

				if (source != null)
				{
					return source.Value;
				}

				return Thread.CurrentPrincipal.Identity;
			};

		public static bool IsAuthenticated
		{
			get
			{
				return CurrentUser.Identity().IsAuthenticated;
			}
		}

		public static string Name
		{
			get
			{
				return CurrentUser.Identity().Name;
			}
		}
	}
}
